<?php
require_once '../models/Product.php';

$action = $_REQUEST['action'];
if ($action == "add-product"){
	$checknull=true;
	$brandID 	= $_REQUEST['brandID'];
	$modelID 	= $_REQUEST['modelID'];
	$seriesID 	= $_REQUEST['seriesID'];
	$image		= $_REQUEST['image'];
	$quantity 		= $_REQUEST['quantity'];
	$price		= $_REQUEST['price'];
	$check=Product::checkProduct($brandID,$modelID,$seriesID);
	
	if($brandID==0||$modelID==0||$seriesID==0||$image==null||$quantity==0||$price==0)
	{
	$checknull=false;
	}
	if($checknull==true){
	if ($check == true){
		$re = Product::insertProduct($brandID,$modelID,$seriesID,$image,$quantity,$price);
		if ($re){
					
		
			print("<span class='ok'>Product is successfully inserted.</span>");
			echo "<a href='../admin/product_detail.php?seriesID=$seriesID'>Nhập chi tiết sản phẩm</a>";
		}
		else print('<span class="error">System is busy. Please try again later.</span>');	
	}
	else print ('<span class="warning">This Product is already in use.</span>');
	}else echo "<span class='warning'>Not null!!!.</span>";
}
if ($action == "update-product"){
	$checknull=true;
	$productID= $_REQUEST['productID'];
	$brandID 	= $_REQUEST['brandID'];
	$modelID 	= $_REQUEST['modelID'];
	$seriesID 	= $_REQUEST['seriesID'];
	$image		= $_REQUEST['image'];
	$quantity 		= $_REQUEST['quantity'];
	$price		= $_REQUEST['price'];
	$status_pr=	$_REQUEST['status_pr'];
	//$check=Product::checkProduct($brandID,$modelID,$seriesID);
		$check=true;
	if($brandID==0||$modelID==0||$seriesID==0||$image==null||$quantity==0||$price==0)
	{
	$checknull=false;
	}
	if($checknull==true){
	if ($check == true){
		$re = Product::updateProduct($productID,$brandID,$modelID,$seriesID,$image,$quantity,$price,$status_pr);
		if ($re){
			print("<span class='ok'>Product is successfully updated.</span>");
			echo "<a href='../admin/update_product_dt.php?productID=$productID'>Cập nhật chi tiết sản phẩm</a>";
		}
		else print('<span class="error">System is busy. Please try again later.</span>');	
	}
	else print ('<span class="warning">This Product is already in use.</span>');
	}else echo "<span class='warning'>Not null!!!.</span>";
}
if ($action == "add-product-detail"){
	$checknull=true;
	$productID 	= $_REQUEST['productID'];
	$screen 	= $_REQUEST['screen'];
	$cpu 	= $_REQUEST['cpu'];
	$ram		= $_REQUEST['ram'];
	$os 		= $_REQUEST['os'];
	$sim		= $_REQUEST['sim'];
	$camera		= $_REQUEST['camera'];
	$memory_in		= $_REQUEST['memory_in'];
	$memory_out		= $_REQUEST['memory_out'];
	$cell		= $_REQUEST['cell'];

	if($screen==null||$cpu===null||$ram==null||$os==null||$sim==null||$camera==null||$memory_in==null||$memory_out==null||$cell==null)
	{
	$checknull=false;
	}
	$check=Product::checkProductDetail($productID);
	if($checknull==true){
	if ($check == true){
		$re = Product::insertProductDetail($productID,$screen,$cpu,$ram,$os,$sim,$camera,$memory_in,$memory_out,$cell);
		if ($re){
			print("<span class='ok'>Product Detail is successfully inserted.</span>");
		}
		else print('<span class="error">System is busy. Please try again later.</span>');	
	}
	else print ('<span class="warning">This Product is already in use.</span>');
	}else echo "<span class='warning'>Not null!!!.</span>";
}
if ($action == "update-product-dt"){
	$checknull=true;
	$check=true;
	$productID 	= $_REQUEST['productID'];
	$screen 	= $_REQUEST['screen'];
	$cpu 	= $_REQUEST['cpu'];
	$ram		= $_REQUEST['ram'];
	$os 		= $_REQUEST['os'];
	$sim		= $_REQUEST['sim'];
	$camera		= $_REQUEST['camera'];
	$memory_in		= $_REQUEST['memory_in'];
	$memory_out		= $_REQUEST['memory_out'];
	$cell		= $_REQUEST['cell'];

	if($screen==null||$cpu===null||$ram==null||$os==null||$sim==null||$camera==null||$memory_in==null||$memory_out==null||$cell==null)
	{
	$checknull=false;
	}
	//$check=Product::checkProductDetail($productID);
	if($checknull==true){
	if ($check == true){
		$re = Product::updateProductDt($productID,$screen,$cpu,$ram,$os,$sim,$camera,$memory_in,$memory_out,$cell);
		if ($re){
			print("<span class='ok'>Product Detail is successfully updated.</span>");
		}
		else print('<span class="error">System is busy. Please try again later.</span>');	
	}
	else print ('<span class="warning">This Product is already in use.</span>');
	}else echo "<span class='warning'>Not null!!!.</span>";
}?>